package com.iandun.web.framework.core.validation;

import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 验证参数工具类
 *
 * @author :jason.liang
 * @date: 2021-05-20 08:32
 */
@Slf4j
public class ValidationUtils {

    private final static Validator validation =
            Validation.buildDefaultValidatorFactory()
                    .getValidator();

    /**
     * 验证对象是否符合JSR303规范
     *
     * @param t 参与验证的对象
     * @return 返回异常的错误提示
     */
    public static <T> String validate(T t) {
        Set<ConstraintViolation<T>> validate = validation.validate(t);
        return validate.stream()
                .map(x -> x.getPropertyPath() + " " + x.getMessage())
                .collect(Collectors.joining(","));
    }
}
